﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;

namespace ConsoleApp1.Work_5
{
    static class work53
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }

    public class MainForm : Form
    {
        private TextBox txtFile1;
        private TextBox txtFile2;
        private Button btnBrowse1;
        private Button btnBrowse2;
        private Button btnMerge;
        private Label lblStatus;

        public MainForm()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.Text = "文件合并工具";
            this.Size = new Size(600, 250);
            this.StartPosition = FormStartPosition.CenterScreen;

            Label lbl1 = new Label { Text = "选择文件1：", Location = new Point(20, 20), AutoSize = true };
            txtFile1 = new TextBox { Location = new Point(100, 16), Width = 350 };
            btnBrowse1 = new Button { Text = "浏览...", Location = new Point(470, 14) };
            btnBrowse1.Click += BtnBrowse1_Click;

            Label lbl2 = new Label { Text = "选择文件2：", Location = new Point(20, 60), AutoSize = true };
            txtFile2 = new TextBox { Location = new Point(100, 56), Width = 350 };
            btnBrowse2 = new Button { Text = "浏览...", Location = new Point(470, 54) };
            btnBrowse2.Click += BtnBrowse2_Click;

            btnMerge = new Button { Text = "合并并保存", Location = new Point(240, 100), Width = 100, Height = 30 };
            btnMerge.Click += BtnMerge_Click;

            lblStatus = new Label { Text = "", Location = new Point(20, 150), AutoSize = true, ForeColor = Color.Blue };

            this.Controls.AddRange(new Control[] { lbl1, txtFile1, btnBrowse1, lbl2, txtFile2, btnBrowse2, btnMerge, lblStatus });
        }

        private void BtnBrowse1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    txtFile1.Text = ofd.FileName;
                }
            }
        }

        private void BtnBrowse2_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    txtFile2.Text = ofd.FileName;
                }
            }
        }

        private void BtnMerge_Click(object sender, EventArgs e)
        {
            lblStatus.Text = "";
            string path1 = txtFile1.Text;
            string path2 = txtFile2.Text;

            try
            {
                if (string.IsNullOrWhiteSpace(path1) || string.IsNullOrWhiteSpace(path2))
                    throw new ApplicationException("请先选择两个文件。");
                if (!File.Exists(path1) || !File.Exists(path2))
                    throw new FileNotFoundException("所选文件不存在。");

                // 读取文件内容
                string content1 = File.ReadAllText(path1);
                string content2 = File.ReadAllText(path2);

                // 创建Data目录
                string dataDir = Path.Combine(Application.StartupPath, "Data");
                if (!Directory.Exists(dataDir))
                    Directory.CreateDirectory(dataDir);

                // 新文件名（时间戳）
                string fileName = $"Merged_{DateTime.Now:yyyyMMdd_HHmmss}.txt";
                string outPath = Path.Combine(dataDir, fileName);

                // 合并并写入
                File.WriteAllText(outPath, content1 + Environment.NewLine + content2);

                lblStatus.ForeColor = Color.Green;
                lblStatus.Text = $"合并成功！文件已保存到: {outPath}";
            }
            catch (Exception ex)
            {
                lblStatus.ForeColor = Color.Red;
                lblStatus.Text = "错误: " + ex.Message;
            }
        }
    }
}
